package android.money;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.money.db.AccountManager;
import android.money.db.DBInfo;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.MenuItem;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class AccountList extends ListActivity {
	private static final int ADD_PAGE = Menu.FIRST;
	private AccountManager accManager;
	@Override
    public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		accManager = new AccountManager(this);
		accManager.open();
		fillData();
	}
	private void fillData() {
		// TODO Auto-generated method stub
		Cursor accountCursor = accManager.fetchAllAccounts();
		startManagingCursor(accountCursor);
		
        ListAdapter adapter = new SimpleCursorAdapter(this,
                android.R.layout.simple_list_item_2, accountCursor, 
                        new String[] { DBInfo.account_name, DBInfo.account_number }, 
                        new int[] { android.R.id.text1, android.R.id.text2 });
        setListAdapter(adapter);
	}
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);
		Intent i = new Intent(this, AccountEdit.class);
		i.putExtra(DBInfo.account_id, (int)id);
		startActivity(i);
	}
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, ADD_PAGE,0,R.string.menu_add).setIcon(android.R.drawable.ic_menu_add);
		return true;
	}
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
    	super.onMenuItemSelected(featureId, item);
    	switch(item.getItemId()) {
    	case ADD_PAGE:
    		startActivity(new Intent(this, AccountAdd.class));
    		break;
    	}
    	return true;
    }
}
